// index.js
const app = getApp()

Page({
  data: {
    canDraw: true, // 是否可以抽奖，默认为true
    index: 0,
    imgArr: [
        '/pages/huodong/youhuiquan/image/gxhdyccjjh.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
        '/pages/huodong/youhuiquan/image/xxhg.jpg',
         '../youhuiquan/image/mfpsq.jpg',
         '../youhuiquan/image/mfpsq.jpg',
         '../youhuiquan/image/mfpsq.jpg',
         '../youhuiquan/image/mfpsq.jpg',
         '../youhuiquan/image/rnbdyj.jpg',
         '../youhuiquan/image/rnbdyj.jpg',
         '../youhuiquan/image/rnbdyj.jpg',
         '../youhuiquan/image/rnbdyj.jpg',
         '../youhuiquan/image/spq.jpg',  
         '../youhuiquan/image/spq.jpg',
         '../youhuiquan/image/spq.jpg',
         '../youhuiquan/image/spq.jpg', 
         '../youhuiquan/image/yydyj.jpg',
         '../youhuiquan/image/yydyj.jpg',
         '../youhuiquan/image/yydyj.jpg',
         '../youhuiquan/image/yydyj.jpg',
         '../youhuiquan/image/yydyj.jpg',
    ]
  },
  onLoad: function () {
    // 页面加载时检查用户是否已经抽奖过
    this.checkUserDrawStatus();
  },
  checkUserDrawStatus: function () {
    // 从本地存储中获取用户的抽奖状态
    const hasDrawn = wx.getStorageSync('hasDrawn') || false;
    if (hasDrawn) {
      this.setData({ canDraw: false });
      wx.showToast({
        title: '您已参加过抽奖',
        icon: 'none',
        duration: 2000
      });
    }
  },
  choujiang: function () {
    if (!this.data.canDraw) {
        wx.showToast({
          title: '您已参加过抽奖',
          icon: 'none',
          duration: 2000
        });
        return;
      }
  
      this.setData({
        index: createRandomIndex()
      });
  
      // 更新用户的抽奖状态到本地存储
      wx.setStorageSync('hasDrawn', true);
  
      wx.showToast({
        title: '恭喜中奖',
        icon: 'success',
        duration: 2000
      });
  
      // 抽奖成功后设置canDraw为false
      this.setData({ canDraw: false });
    }
  });
  // 随机数字向下取整
  function createRandomIndex() {
    return Math.floor(Math.random() * 30);
  }